<!-- 等级编辑弹窗 -->
<template>
  <el-dialog title="批量出库" :visible="visible" :destroy-on-close="true" :lock-scroll="false"
             @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="102px">
      <el-form-item label="操作人员:" prop="operator_id">
        {{user.userName}}
<!--        <el-select clearable v-model="form.operator_id" placeholder="请选择" class="ele-fluid">-->
<!--          <el-option v-for="item in operateList" :key="item.id" :label="item.name" :value="item.id" />-->
<!--        </el-select>-->
      </el-form-item>
<!--      <el-form-item label="出库数量:" prop="shipmentQuantity">-->
<!--        <el-input-number v-model="form.shipmentQuantity" :min="0" :max="minOutboundQuantity"  size="small" />-->
<!--        <span style="font-size: 12px"> 所选产品最低库存数<span style="color: red;">{{ minOutboundQuantity }}</span></span>-->
<!--      </el-form-item>-->
      <el-form-item label="出库数量:" prop="shipmentQuantity">
        <el-input   placeholder="请输入出库数量"
                    clearable v-model="form.shipmentQuantity"    />
      </el-form-item>
      <el-form-item label="出库状态:" prop="shippingType">
        <el-select clearable v-model="form.shippingType" placeholder="请选择" class="ele-fluid">
          <el-option v-for="item in OutboundstatusList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div style="margin-top:20px">
      <i class="el-icon-user icon"></i>
      选择的出库产品
    </div>
    <div class="table-container">
      <el-table :data="form.formData" border style="width: 100%">
        <el-table-column fixed prop="materialId" label="产品ID" width="80">
        </el-table-column>
        <el-table-column prop="materialName" label="产品名称"> </el-table-column>
        <el-table-column prop="materialNumber" label="库位编号"> </el-table-column>
        <el-table-column prop="magnitude" label="库存数量"> </el-table-column>
        <el-table-column prop="unit" label="单位"> </el-table-column>
        <el-table-column prop="weight" label="单价"> </el-table-column>
<!--        <el-table-column prop="validDays" label="有效天数"> </el-table-column>-->
      </el-table>
    </div>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import{allOutbound} from "../../../api/OutboundManagement/otherOutbound";
import {listInventoryInquiry,addInventoryInquiry} from "@/api/wareManagement/inventoryInquiry";
import {getUserProfile} from "../../../api/system/user";
export default {
  name: "",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },
  data() {
    return {
      product_id: [],
      form: {

      },
      formData: {},
      //占位产品
      //   formData:{},
      // 乘客信息

      // 客户表格
      customList: [],
      // 操作人员
      operateList: [
        { name: "操作人A", id: 1 },
        { name: "操作人B", id: 2 },
        { name: "操作人C", id: 3 },
        { name: "操作人D", id: 4 },
        { name: "操作人E", id: 5 },
      ],
      // 表单数据
      // form: Object.assign({ status: 1 }, this.data),
      // 表单验证规则
      rules: {
        shippingType: [
          { required: true, message: "出库状态不能为空", trigger: "blur" },
        ],
        shipmentQuantity: [
          { required: true, message: "出库数量不能为空", trigger: "blur" },
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 最小出库数量
      minOutboundQuantity: 0,
      user:'',
      //出库状态
      OutboundstatusList:[
        { name: "领料出库", id: 1 },
        // { name: "调拨出库", id: 2 },
        { name: "外采出库", id: 2 },
        { name: "退货出库", id: 3 },
        { name: "销售出库", id: 4 },
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 20,

      },
      dataList:[]
    };

  },

  watch: {},
  mounted() {
    //console.log("传过来的参数", this.data.currentall, "datadata");
    if (this.data && this.data.currentall && this.data.currentall.length > 0) {
      // this.getOutbound();
      this.$set(this.form,'formData',this.data.currentall);
    } else {
      console.error("data or data.currentall is not defined or empty");
    }
    this.getUser()
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        //console.log("user",this.user)
      });
    },
    dd(value) {
      this.form.validDays = value.replace(/\D/g, '');
    },
    // 获取选择的出库产品
    // getOutbound(){
    //   //console.log("传参数",this.data.currentall)
    //   const material_id = this.data.currentall
    //   this.queryParams.deliveryId = material_id
    //   allOutbound(  this.queryParams).then(res => {
    //     //console.log("98989898989",res)
    //     this.$set(this.form,'formData',res.rows);
    //     //console.log("this.form.formData",this.form,"data",this.form.formData)
    //     this.loading = false;
    //     this.setMinOutboundQuantity();
    //   });
    // },
   // 已选出库产品的最低库存数
    setMinOutboundQuantity(){
      if (this.form.formData && this.form.formData.length > 0) {
        const minQuantity = Math.min(...this.form.formData.map(item => item.nventoryQuantity));
        this.minOutboundQuantity = minQuantity > 1 ? minQuantity : 1;
      } else {
        this.minOutboundQuantity = 1;
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid1) => {
        if (valid1) {
          this.form.formData.forEach((item) => {
            this.dataList.push(item.deliveryId);
          });
          // //console.log("this.form.formData", this.dataList,"this.form",this.form);
          const canshuList = {}
          // canshuList.deliveryId = this.dataList.join(',');
          // canshuList.deliveryId = this.dataList;
          // canshuList.shippingType = this.form.shippingType
          // canshuList.shipmentQuantity = this.form.shipmentQuantity
          // this.form.zhijian = 1
          // //console.log("canshuList",canshuList)
          // this.form.zhijian = 1
          //console.log("99666",this.form)
          addInventoryInquiry(this.form).then(res => {
            //console.log("resresres",res)
            if(res.code === 200){
              this.$modal.msgSuccess({
                message: res.msg,
                type: 'success',
              });
              this.updateVisible(false);
              this.$emit("done");
            }else{
              this.$modal.msgSuccess({
                message: res.msg,
                type: 'error',
              });
            }
          });
        } else {
          this.$message.error("表单填写有误，请检查必填项");
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      (this.form = {
      })
        this.$emit("update:visible", value);
    },
  },
};
</script>

<style scoped>
.table-container {
  padding: 20px;
  border: 1px solid #ccc;
  /* 添加边框 */
  margin: 20px;
}

.icon {
  font-size: 15px;
  margin-left: 10px;
}
</style>
